package cn.fl.custpay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.user.facade.framework.constant.DictionaryConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.custpay.api.ICustPayCfgDApiService;
import cn.fl.custpay.condition.CustPayCfgDQC;
import cn.fl.custpay.dto.CustPayCfgDDTO;
import cn.fl.custpay.service.ICustPayCfgDService;
import cn.fl.custpay.transform.CustPayCfgDVOTransform;
import cn.fl.custpay.vo.CustPayCfgDVO;
import cn.fl.system.service.ISysDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 支付渠道配置子表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CustPayCfgDServiceImpl implements ICustPayCfgDService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ICustPayCfgDService.class);

	/**
	 * 渠道暴漏API
	 */
	@Autowired
	private ICustPayCfgDApiService custPayCfgDApiService;

	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Override
	public CustPayCfgDVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		CustPayCfgDDTO dto = this.custPayCfgDApiService.selectById(id);
		return CustPayCfgDVOTransform.toVO(dto);
	}

	@Override
	public List<CustPayCfgDVO> searchList(CustPayCfgDQC qc) throws BaseException{
		if(null == qc){
			BaseLogUtils.info(logger, "selectByRelationId", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		List<CustPayCfgDDTO> custPayCfgDDTOList = this.custPayCfgDApiService.searchList(qc);
		List<CustPayCfgDVO> list = CustPayCfgDVOTransform.toVOList(custPayCfgDDTOList);
		//数据字典（是否启用）
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
		Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
		List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
		Map<String, String> statusMap = null;
		if (CollectionUtils.isNotEmpty(statusList)) {
			statusMap = new HashMap<>(statusList.size());
			for (KeyValueVO dto : statusList) {
				statusMap.put(dto.getKey(), dto.getValue());
			}
		}
		if (CollectionUtils.isNotEmpty(list)) {
			for (CustPayCfgDVO vo : list) {
				vo.setIsValidNm(statusMap.get(String.valueOf(vo.getIsValid())));
				if (StringUtils.isNotEmpty(vo.getOnceLimit())) {
					vo.setOnceLimitNm(DecimalUtils.convertFenToYuan(vo.getOnceLimit()));
				}
				if (StringUtils.isNotEmpty(vo.getDayLimit())) {
					vo.setDayLimitNm(DecimalUtils.convertFenToYuan(vo.getDayLimit()));
				}
			}
		}
		return list;
	}

	@Override
	public Long add(CustPayCfgDVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.custPayCfgDApiService.add(CustPayCfgDVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.custPayCfgDApiService.removeById(id);
	}

	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(CustPayCfgDVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.custPayCfgDApiService.modify(CustPayCfgDVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.custPayCfgDApiService.searchListPage(page);
	}

	@Override
	public Boolean modifyRDIsvalid(Long id, int isValid) {
		if(null == id){
			BaseLogUtils.info(logger, "modifyRDIsvalid", "修改支付渠道管理子表状态失败,支付渠道管理子表对象参数为空.DATA:"+ id, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.custPayCfgDApiService.modifyRDIsValid(id, isValid);
	}
}
